package rejjie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import rejjie.common.BaseContext;
import rejjie.common.R;
import rejjie.entity.ShoppingCart;
import rejjie.service.ShoppingCartService;

import java.security.URIParameter;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 忘购物车中添加数据
     * 1. 先从shoppingcart表中查询，
     * 2. 如果不能查询到，就直接进行插入
     * 3. 如果能够查询到，就更新 查询到的食品的数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    @Transactional
    public R<ShoppingCart> addCart(@RequestBody  ShoppingCart shoppingCart){
        log.info("{}",shoppingCart);
        // 得到当前登录的用户id
        Long currentID = BaseContext.getCurrentID();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLQW = new LambdaQueryWrapper();
        shoppingCartLQW.eq(ShoppingCart::getUserId,currentID);

        Long dishId = shoppingCart.getDishId();
        if(dishId !=null){
            // 证明传过来的参数中有菜品
            shoppingCartLQW.eq(ShoppingCart::getDishId,dishId);
        }else{
            // 证明穿过来的参数没有菜品，但是传过来了 套餐id
            // 两者总要穿过来一个
            Long setmealId = shoppingCart.getSetmealId();
            shoppingCartLQW.eq(ShoppingCart::getSetmealId,setmealId);
        }

        // 进行查询
        ShoppingCart currentshopcart = shoppingCartService.getOne(shoppingCartLQW);

        if(currentshopcart!=null){
            // 证明有相同的菜品， 需要做的事更新数目 number
            currentshopcart.setNumber(currentshopcart.getNumber()+1);
            shoppingCartService.updateById(currentshopcart);
        }else{
            // 证明没有 插入一条数据
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(currentID);
            shoppingCartService.save(shoppingCart);
            currentshopcart=shoppingCart;
        }


        return R.success(currentshopcart);
    }

    /**
     * 查看购物扯相关信息
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>>  getList(){
        log.info("查看购物车");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentID());
        shoppingCartLambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> shoplist = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(shoplist);
    }

    /**
     * 删除购物车中的菜品
     * 因为 传过来的参数有 dishID 和 setmealID
     * 需要判断传过来的是那种id, 然后对其进行擦做
     * 让表中的数目减1
     * @Param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    @Transactional
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart){
        log.info("菜品id{},套餐id{}",shoppingCart.getDishId(),shoppingCart.getSetmealId());
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Long currentID = BaseContext.getCurrentID();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,currentID);
        shoppingCart.setUserId(currentID);

        // 设置查询条件
        if(shoppingCart.getDishId()!=null) {
            shoppingCartLambdaQueryWrapper.eq( ShoppingCart::getDishId, shoppingCart.getDishId());

        }else {
            shoppingCartLambdaQueryWrapper.eq( ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 根据设置的条件查找表
        shoppingCart  = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);

        //我们还要对表中数据进行判断 如果个数达到了0 就进行删除 ，否则才进行更新
        Integer number = shoppingCart.getNumber()-1;
        if(number==0){
            //进行删除     条件可以服用
            shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        }
        shoppingCart.setNumber(number);
        shoppingCartService.updateById(shoppingCart);
        return R.success(shoppingCart);
    }


}
